package config

import (
	"github.com/spf13/viper"
)

const config_file_path = "/Users/mi/code/test/go-web-api/config/"

type ServerConfig struct {
	Server Server
}
type Server struct {
	Port string `mapstructure:"port"`
}

func LoadConfig(env string) (*ServerConfig, error) {
	fullPath := "config-" + env + ".yaml"
	viper.AddConfigPath(config_file_path)
	viper.SetConfigName(fullPath)
	viper.SetConfigType("yaml") // 设置配置文件类型
	err := viper.ReadInConfig()
	if err != nil {
		return nil, err
	}

	serverConfig := &ServerConfig{}
	err = viper.Unmarshal(serverConfig)
	if err != nil {
		return nil, err
	}

	return serverConfig, nil
}
